//
//  CJExten+UINavi.swift
//  CJCore
//
//  Created by 陈警卫 on 2024-07-05.
//

import UIKit
import Foundation

public extension UINavigationController {
    
    /// 获取最前的UINavigationController
    @objc class func cj_currentNC(_ vc: UIViewController?) -> UINavigationController? {
        if let tc = vc {
            if tc.isKind(of: UITabBarController.self) {
                let vc = (tc as! UITabBarController).selectedViewController
                return cj_currentNC(vc)
            } else if tc.isKind(of: UINavigationController.self) {
                if let vc = (tc as! UINavigationController).presentedViewController {
                    return cj_currentNC(vc)
                }
                let vc = (tc as! UINavigationController).topViewController
                return cj_currentNC(vc)
            } else if tc.isKind(of: UIViewController.self) {
                if let vc = vc?.presentedViewController {
                    return cj_currentNC(vc)
                } else {
                    return tc.navigationController
                }
            } else {
                return nil
            }
        } else {
            let vc = UIScreen.cj_window?.rootViewController
            return cj_currentNC(vc)
        }
    }
    
    /// 获取已push的UIViewController
    @objc func cj_vcPushs(name: String) -> UIViewController? {
        guard let vcClass = NSClassFromString(name) else { return nil}
        for vc in self.viewControllers {
            if vc.isKind(of: vcClass) {
                return vc
            }
        }
        return nil
    }
    
}
